package controller;


import java.io.IOException;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import log.MyLogger;


public class RemonditeenusServlet extends HttpServlet {
	
	//static Logger
	
	public  void init() {
			
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		//res.setContentType("text/html");
		
		//MyLogger.LogMessage("CellphoneServlet:init() - now I'm created!");
		HttpSession session = req.getSession(true);
		LoginController login = new LoginController();
		
		if (req.getParameter("action") != null && req.getParameter("action").equalsIgnoreCase("logout")) {
			HttpSession destry_session = req.getSession(false);
			if (destry_session != null) {
				destry_session.invalidate();
			}
			res.sendRedirect("/r56_remonditeenus/s");
			return;
		}
		
		
		if (session.getAttribute("userId") != null) {
			
			if (req.getParameter("action") != null && req.getParameter("action").equalsIgnoreCase("service")) {
				new ServiceController(req, res, getServletConfig().getServletContext(), session);
				return;
			}
			
			if (req.getParameter("action") != null && req.getParameter("action").equalsIgnoreCase("search")) {
				new SearchController(req, res, getServletConfig().getServletContext(), session);
				return;
			}
			
			
			getServletConfig().getServletContext().getRequestDispatcher("/s.jsp").forward(req, res);
			return;
		}
		else {
			login.login(req, res, getServletConfig().getServletContext(), session);
			return;
		}		


		//ServletContext sc = getServletConfig().getServletContext();
		//req.setAttribute ("resultAll", cp);
		//CellphoneDAO CellphoneDAO = new CellphoneDAO();
		//Cellphone cp = CellphoneDAO.findAll();
		//out.println(cp.getName());
		//Cellphone resultCellphone = new Cellphone();
		//Cellphone resultCellphone = CellphoneDAO.findById(1);
		//req.setAttribute ("resultCellphone", resultCellphone);
		//
	}
	
	public String getServletInfo() {
		return "RemonditeenusServlet";
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
		
		doGet(request, response);
	}
}
